#import "OmMomentsVCTableViewCellImagesView.h"
#import <HUPhotoBrowser.h>
#define Margin 20
#define CountForRow 3
@interface OmMomentsVCTableViewCellImagesView ()
@property (nonatomic, strong) NSMutableArray<UIImageView *> *imageViews;
@end
@implementation OmMomentsVCTableViewCellImagesView
#pragma mark - Event
- (void)setImageModelArray:(NSArray<OmPictureModelMKII *> *)imageModelArray {
    _imageModelArray = imageModelArray;
    [self cleanImageViews];
    [self allocateImageViews];
    [self layoutImageViews];
}
- (void)onSelfTap {
    NSMutableArray *imageUrls = [NSMutableArray array];
    for (OmPictureModelMKII *model in self.imageModelArray) {
        [imageUrls addObject:[OmHandyMethods resizedImageUrlwith:model.image_url width:@(WINDOW_WIDTH*2) height:nil]];
    }
    UIImageView *imageView = [[UIImageView alloc] init].fp_contentMode(UIViewContentModeScaleAspectFit);
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrls[0]]];
    [HUPhotoBrowser showFromImageView:imageView withURLStrings:imageUrls atIndex:0];
}
#pragma mark - Method
- (void)cleanImageViews {
    for (UIImageView *imageView in self.imageViews) {
        [imageView removeFromSuperview];
    }
    self.imageViews = [NSMutableArray array];
}
- (void)allocateImageViews {
    for (OmPictureModelMKII *model in self.imageModelArray) {
        UIImageView *imageView = (UIImageView *)[[UIImageView alloc] init]
        .fp_contentMode(UIViewContentModeScaleAspectFill)
        .fp_cornerRadius(5)
        .fp_clipsToBounds(YES);
        [imageView sd_setImageWithURL:[NSURL URLWithString:[OmHandyMethods resizedImageUrlwith:model.image_url width:@(200) height:@(200)]]];
        [self addSubview:imageView];
        [self.imageViews addObject:imageView];
    }
}
- (void)layoutImageViews {
    for (int i = 0; i < self.imageViews.count; i++) {
        UIImageView *currentImageView = self.imageViews[i];
        UIImageView *previousImageView = self.imageViews[i-1];
        [currentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.mas_width).multipliedBy(1/3.0).offset(-1.0 * Margin * (CountForRow - 1) / CountForRow);
            make.height.equalTo(currentImageView.mas_width);
            if (i == 0) {
                make.top.left.equalTo(self);
            } else if (i % CountForRow == 0) {
                make.left.equalTo(self);
                make.top.equalTo(previousImageView.mas_bottom).offset(Margin);
            } else {
                make.left.equalTo(previousImageView.mas_right).offset(Margin);
                make.centerY.equalTo(previousImageView);
            }
            if (i == (self.imageViews.count - 1)) {
                make.bottom.equalTo(self);
            }
        }];
    }
}
#pragma mark - ConstructionScript
- (void)configSelf {
    self.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelfTap)];
    [self addGestureRecognizer:recognizer];
}
@end
